package com.cnhis.cloudhealth.clinical.hosnurse.hospatientcase.bo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.hosnurse.hospatientcase.dao.HosPatientCaseDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.JsonUtil;
import com.cnhis.cloudhealth.commons.utils.Page;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;

@Service
public class HosPatientCaseBo
{
    @Resource
    private HosPatientCaseDao hosPatientCaseDao;
    
    @Autowired
    private GetNewId getNewId;
    
    public ModelVo patientCaseList(Map map, int currentPage, int pageSize)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        Page pg = null;
        try
        {
            pg = hosPatientCaseDao.patientCaseList(map, currentPage, pageSize);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        mo.setList(pg.getList());
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setRows(pg.getList());
        mo.setTotal(pg.getTotalNumber());
        return mo;
    }
    
    private Map getBCK01(Map map)
        throws Exception
    {
        return hosPatientCaseDao.getBCK01(map);
    }
    
    @SuppressWarnings("unchecked")
    public ModelVo execEmrSelectUse(Map map)
        throws Exception
    {
        if ("0".equals(map.get("BCK01").toString()))
        {
            Map m = getBCK01(map);
            if (m != null && m.size() > 0)
            {
                map.put("BCK01", m.get("bck01c"));
                map.put("DeptId", m.get("bck01d"));
            }
        }
        ModelVo mo = new ModelVo();
        try
        {
            List list = hosPatientCaseDao.emrSelectUse(map);
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }
    
    public ModelVo getLastHistory(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        List pg = null;
        try
        {
            if ("1".equals(map.get("aACF01").toString()))
            {
                pg = hosPatientCaseDao.getLastHistory(map);
                
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        mo.setList(pg);
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setRows(pg);
        return mo;
        
    }
    
    public ModelVo personalCase(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        List pg = null;
        try
        {
            pg = hosPatientCaseDao.personalCase(map);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        mo.setList(pg);
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setRows(pg);
        return mo;
    }
    
    public ModelVo delPatientVcf1(Map map)
    {
        ModelVo mo = new ModelVo();
        try
        {
            //改为前端校验
            /*Object object = hosPatientCaseDao.queryForObject("HosPatientCase.queryVcf1ByVcf01AndVcf18", map);
            if(object!=null)
            {
                mo.setResultMsg("该护理记录已经归档，不允许删除");
                mo.setResult(StaticKeys.OH_FALSE);
                return mo;
            }*/
            List<Map<String, Object>> vcf1List = JsonUtil.getList(map.get("vcf1").toString());
            map.put("vcf1List", vcf1List);
            hosPatientCaseDao.delPatientVcf1(map);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        
    }

    public ModelVo queryCareCad1(Map hs)
    {
        ModelVo mo = new ModelVo();
        try
        {
            List list = hosPatientCaseDao.queryCareCad1(hs);
            mo.setResult(StaticKeys.OH_SUCCESS);
//            mo.setList(list);
            mo.setRows(list);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo queryCareBck1(Map hs)
    {
        ModelVo mo = new ModelVo();
        try
        {
            List list = hosPatientCaseDao.queryCareBck1(hs);
            mo.setRows(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo queryCareJibie(Map hs)
    {
        ModelVo mo = new ModelVo();
        try
        {
            List list = hosPatientCaseDao.queryCareJibie(hs);
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    @SuppressWarnings("unchecked")
    public ModelVo execEMRMaster_CAD1_Update(Map hs)
    {
        ModelVo mo = new ModelVo();
        try
        {
            Map<String, Object> cad1 = JsonUtil.getMap(hs.get("cad1").toString());
            List<Map<String, Object>> cbd1 = null;
            Object object = hs.get("cbd1");
            long cad01 = Long.parseLong(hs.get("cad01").toString());
            cad1.put("cad01", cad01);
            hs.put("cad1", cad1);
            
            if (object!=null)
            {
                cbd1 = JsonUtil.getList(object.toString());
            }
            Long cad01_new = getNewId.nextId();
            if(cad01>0)//修改
            {
                //判断病历文件是否被是使用
                List exitsList = hosPatientCaseDao.exitsCad1(hs);
                if(!exitsList.isEmpty())
                {
                    mo.setResult(StaticKeys.OH_FALSE);
                    mo.setResultMsg("病历文件名称已经被使用，请重试");
                    return mo;
                }
                else
                {
                    hosPatientCaseDao.updateCad1(hs);
                    for (int i = 0; i < cbd1.size(); i++)
                    {
                        cbd1.get(i).put("cbd01", getNewId.nextId());
                        cbd1.get(i).put("cad01", cad01);
                    }
                    if (cbd1!=null && cbd1.size()>0)
                    {
                        hs.put("cbd1", cbd1);
                        hosPatientCaseDao.updateCbd1(hs);
                    }
                }
                
            }
            else if(cad01==0)//新增
            {
                ((Map)hs.get("cad1")).put("cad01", cad01_new);
                hosPatientCaseDao.insertCad1(hs);
                for (int i = 0; i < cbd1.size(); i++)
                {
                    cbd1.get(i).put("cbd01", getNewId.nextId());
                    cbd1.get(i).put("cad01", cad01_new);
                }
                if (cbd1!=null && cbd1.size()>0)
                {
                    hs.put("cbd1", cbd1);
                    hosPatientCaseDao.updateCbd1(hs);
                }
            }
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo execEMRMaster_CAZ1_Configs(Map map)
    {
        ModelVo mo = new ModelVo();
        try
        {
            List<Map<String, Object>> cbsList =  JsonUtil.getList(map.get("cbs1").toString());
            String cad01 = map.get("cad01").toString();
            for (int i = 0; i < cbsList.size(); i++)
            {
                Map<String, Object> teMap = cbsList.get(i);
                teMap.put("cbs01", getNewId.nextId());
                teMap.put("cad01", cad01);
                teMap.put("cbf01", 0);
                teMap.put("cbs04", 0);
                if("".equals(teMap.get("cbs14").toString()))
                {
                    teMap.put("cbs14", 100);
                }
            }
            if (cbsList!=null && cbsList.size()>0)
            {
                map.put("cbsList", cbsList);
                hosPatientCaseDao.execEMRMaster_CAZ1_Configs(map);
            }
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo queryAllCaz1(Map map)
    {
        ModelVo mo = new ModelVo();
        try
        {
            List list = hosPatientCaseDao.queryAllCaz1(map);
            mo.setRows(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo queryCaz16(Map map)
    {
        ModelVo mo = new ModelVo();
        try
        {
            List list = hosPatientCaseDao.queryCaz16(map);
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo queryCareKeshi(Map map)
    {
        ModelVo mo = new ModelVo();
        try
        {
            List list = hosPatientCaseDao.queryCareKeshi(map);
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo execEMRMaster_CAZ1_Update(Map map)
    {
        ModelVo mo = new ModelVo();
        try
        {
            Map<String, Object> caz1 = JsonUtil.getMap(map.get("caz1").toString());
            List<Map<String, Object>> cba1List = JsonUtil.getList(map.get("cba1").toString());
            for (int j = 0; j < cba1List.size(); j++)
            {
                Map teMap = cba1List.get(j);
                teMap.put("cba01", getNewId.nextId());
                teMap.put("caz01", map.get("caz01"));
            }
            map.put("cba1List", cba1List);
            map.put("caz1", caz1);
            
            hosPatientCaseDao.execEMRMaster_CAZ1_Update(map);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo execEMRMaster_CAD1_Delete(Map map)
    {
        ModelVo mo = new ModelVo();
        try
        {
            List<Map<String, Object>> cad1 = JsonUtil.getList(map.get("cad1").toString());
            map.put("cad1", cad1);
            hosPatientCaseDao.execEMRMaster_CAD1_Delete(map);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo getCareItem(HashMap map)
    {
        ModelVo mo = new ModelVo();
        try
        {
            List list = hosPatientCaseDao.getCareItem(map);
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo getCazBck1(HashMap map)
    {
        ModelVo mo = new ModelVo();
        try
        {
            List list = hosPatientCaseDao.getCazBck1(map);
            mo.setRows(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo queryCareAllCab1(HashMap hs)
    {
        ModelVo mo = new ModelVo();
        try
        {
            List list = hosPatientCaseDao.queryForList("HosPatientCase.queryCareAllCab1", hs);
            mo.setRows(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    
}
